﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Sublimation
{
    public class TowerEnergy : GameObject
    {
        protected Tower parent;
        public Tower Parent
        {
            get { return this.parent; }
        }

        protected float amount;
        public float Amount
        {
            get { return this.amount; }
            set { this.amount = MathHelper.Clamp(value, 0, MaxAmount); }
        }

        protected float maxAmount;
        public float MaxAmount
        {
            get { return this.maxAmount; }
            set { this.maxAmount = Math.Max(value, 0); }
        }

        protected float energyPerSecond;
        public float EnergyPerSecond
        {
            get { return this.energyPerSecond; }
            set { this.energyPerSecond = value; }
        }

        public const float DEFAULT_MAX_AMOUNT = 7500f; // max health
        public const float DEFAULT_BUILDING_AMOUNT = 3000f; // building is built here, first tower has this
        public const float DEFAULT_AMOUNT = 1000f; // amount an unbuild tower starts with
        public const float DEFAULT_ENERGY_PER_SECOND = 105f; // energy income
        private const float ENERGY_SCALE_TO_PARENT = 0.8f;

        public TowerEnergy(WorldProperties worldProperties, Tower parent)
            : base(worldProperties)
        {
            LoadContent();

            // make sure it loads and draws
            DrawOrder = 80;
            UpdateOrder = 55;
            Visible = true;
            Enabled = true;

            Origin = Vector2.Zero;
            this.parent = parent;
            this.Scale = 1.0f / SPRITE_SCALE_TO_ONE;
            ScaleWithinObject(parent, ENERGY_SCALE_TO_PARENT);
            this.Velocity = Vector2.Zero;

            CentreOnObject(parent);

            MaxAmount = DEFAULT_MAX_AMOUNT;
            if (parent.Building)
            {
                Amount = DEFAULT_AMOUNT;
            }
            else
            {
                Amount = DEFAULT_BUILDING_AMOUNT;
            }
            EnergyPerSecond = DEFAULT_ENERGY_PER_SECOND;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            // mirror the parents movements
            CentreOnObject(parent);
            AlignOriginWithObject(parent);
            this.Rotation = parent.Rotation;

            if (!parent.Building)
            {
                // regenerate energy
                Amount += EnergyPerSecond * (float)gameTime.ElapsedGameTime.TotalSeconds;
                //a building slot removes 1/6 of the production
                foreach (Slot slot in parent.Slots)
                {
                    if (slot.Building)
                    {
                        Amount -= (EnergyPerSecond/6) * (float)gameTime.ElapsedGameTime.TotalSeconds;
                    }
                }
            }

            // update the display to reflect the amount of energy
            //TODO: make this a glowy particle effect
            //this.colour.A = (byte) MathHelper.Lerp(0f, 200f, Amount / MaxAmount);

            this.colour.A = (byte)120;
        }

        private Color colour = new Color(255, 255,   255, 200);
        public override void Draw(GameTime gameTime)
        {
            //base.Draw(gameTime);
            
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            if (parent.Building)
            {
                spriteBatch.Draw(Sprite, Position, null, Color.White, Rotation, Origin, (float)Math.Sqrt(amount / DEFAULT_BUILDING_AMOUNT) * Scale, SpriteEffects.None, 0);
            }
            else
            {
                spriteBatch.Draw(Sprite, Position, null, colour, Rotation, Origin, (float)Math.Sqrt(amount / maxAmount) * Scale, SpriteEffects.None, 0);
            }
            spriteBatch.End();
        }

        private const float SPRITE_SCALE_TO_ONE = 50f;
        protected override void LoadContent()
        {
            base.LoadContent();

            Sprite = Game.Content.Load<Texture2D>("Sprites\\tower_energy");
        }

        public override void ApplyDamage(float damage)
        {
            base.ApplyDamage(damage);           
            Amount -= damage;
            if (Amount == 0) Parent.Kill();
        }
    }
}
